package com.young.common.core.net;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collection;
import java.util.Enumeration;
import java.util.TreeMap;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:18 PM
 */
public final class InetTool {
    private static final String LOCAL_HOST_IP = "127.0.0.1";
    private static final String LOCAL_HOST_NAME = "localhost";
    public static final String HOST_NAME_SP_KEY = "HOST_NAME";
    public static final String LOCAL_IP_SP_KEY = "LOCAL_IP";
    public static final String HOST_NAME = hostName();
    public static final String HOST_NAME_FQN = hostName(true);
    public static final String LOCAL_IP = localIp();
    public static final boolean CONTAINERIZED = isContainerized();
    private static final String C_PART = "-pod-";

    public InetTool() {
    }

    public static String getHostName() {
        return getHostName(false);
    }

    public static String getHostName(boolean fqn) {
        return fqn ? HOST_NAME_FQN : HOST_NAME;
    }

    public static String getLocalIp() {
        return LOCAL_IP;
    }

    public static Collection<InetAddress> getAddresses() {
        TreeMap result = new TreeMap();

        try {
            Enumeration interfaces = NetworkInterface.getNetworkInterfaces();

            label41:
            while(true) {
                NetworkInterface network;
                do {
                    if (!interfaces.hasMoreElements()) {
                        break label41;
                    }

                    network = (NetworkInterface)interfaces.nextElement();
                } while(!network.isUp());

                int index = network.getIndex();
                Enumeration addresses = network.getInetAddresses();

                while(addresses.hasMoreElements()) {
                    InetAddress address = (InetAddress)addresses.nextElement();
                    if (checkAddress(address)) {
                        result.put(index, address);
                    }
                }
            }
        } catch (IOException var7) {
        }

        try {
            if (result.isEmpty()) {
                result.put(0, InetAddress.getLocalHost());
            }
        } catch (IOException var6) {
        }

        return result.values();
    }

    public static boolean isContainerized(String hostName) {
        return containerPartIndex(hostName) != -1;
    }

    public static boolean isLocal() {
        return LOCAL_IP.equals("127.0.0.1") || HOST_NAME.equals("localhost");
    }

    private static String localIp() {
        return hostAddress().getHostAddress();
    }

    private static String hostName() {
        return hostName(false);
    }

    private static String hostName(boolean fqn) {
        try {
            String hostName = InetAddress.getLocalHost().getHostName();
            if (fqn) {
                return hostName;
            } else {
                int i = containerPartIndex(hostName);
                return i == -1 ? hostName : hostName.substring(0, i);
            }
        } catch (IOException var3) {
            return "localhost";
        }
    }

    protected static int containerPartIndex(String hostName) {
        int i = hostName.lastIndexOf(".");
        if (i != -1 && i != hostName.length() - 1) {
            String suffix = hostName.substring(i);
            return suffix.contains("-pod-") ? i : -1;
        } else {
            return -1;
        }
    }

    public static boolean isContainerized() {
        return isContainerized(HOST_NAME_FQN);
    }

    private static InetAddress hostAddress() {
        return (InetAddress)getAddresses().iterator().next();
    }

    private static boolean checkAddress(InetAddress address) {
        return !address.isLoopbackAddress() && address.isSiteLocalAddress() && address instanceof Inet4Address;
    }

    public static void main(String[] args) {
        String host1 = "l-mon-nginx1.ops.prod.aws.dm";
        String host2 = "teacher-info-service.pre.ali.ali01-pod-deployment-7b7f4bc5c8-sbr7m";
        System.out.println(isContainerized(host1));
        System.out.println(isContainerized(host2));
        int i = containerPartIndex(host2);
        System.out.println(host2.substring(0, i));
        System.out.println(getHostName());
        System.out.println(getLocalIp());
        System.out.println(isLocal());
        Collection<InetAddress> addresses = getAddresses();
        addresses.forEach((address) -> {
            System.out.println(address.getHostName() + "," + address.getHostAddress());
        });
    }

    static {
        System.setProperty("HOST_NAME", HOST_NAME);
        System.setProperty("LOCAL_IP", LOCAL_IP);
    }
}

